var t = require("../../api.js"), a = getApp();

Page({
    data: null,
    changeName: function (e) {
        // sent data change to view
        this.setData({
            name: 'MINA'
        })
    },
    onLoad: function () {

        a.pageOnLoad(this);
        var o=this;

        // var lat = wx.getStorageSync("pos_latitude");
        // var lng = wx.getStorageSync("pos_longitude");

        o.setData({
            longitude: wx.getStorageSync("pos_longitude"),
            latitude: wx.getStorageSync("pos_latitude")
        });

        if( !o.data.latitude || !o.data.longitude ){
            wx.getLocation({
                success: function(t) {

                    console.log('pos_latitude:' + t.latitude);
                    console.log('pos_longitude:' + t.longitude);

                    o.setData({
                        longitude: t.longitude,
                        latitude: t.latitude
                    });

                    wx.setStorageSync("pos_latitude",t.latitude);
                    wx.setStorageSync("pos_longitude",t.longitude);
                },
                complete: function() {
                    console.log('get location complete');
                    o.get_pickup();
                }
            });
        }else{
            o.get_pickup();
        }

    }

    ,get_pickup : function(){

        var o=this;
        a.request({
            url: t.order.delivery_addr,
            data: {
                pos_latitude : o.data.latitude,
                pos_longitude : o.data.longitude
            },
            success: function (t) {
                console.log(t);
                0 == t.code && o.setData({delivery_list: t.list});

                //todo 计算每一个地址到当前用户的距离
            },
            complete: function () {
                wx.hideLoading()
            }
        });
    }
    ,radioChange: function(e) {
        console.log('radio发生change事件，携带value值为：', e.detail.value);
        wx.setStorageSync("delivery_addr",e.detail.value);
        wx.setStorageSync("delivery_addr_history",Date.parse(new Date()));
        //todo 提货地址代替收货地址 填入该条订单中，使下单流程能够继续走下去
        wx.navigateBack();
    }
});